package com.baidu.mpks.schema.service;


import com.baidu.mpks.common.HttpClientTemplate;
import com.baidu.mpks.schema.dao.UserSchemaClassDao;
import com.baidu.mpks.schema.dao.UserSchemaPropertyDao;
import com.baidu.mpks.schema.domain.UserSchemaClass;
import com.baidu.mpks.schema.domain.UserSchemaProperty;
import com.baidu.mpks.util.JacksonUtils;
import com.fasterxml.jackson.databind.JsonNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.Map;

@Service
public class UserSchemaManageServiceImpl implements UserSchemaManageService {
    private static final Logger log = LoggerFactory.getLogger(UserSchemaManageServiceImpl.class);
    @Autowired
    UserSchemaClassDao userSchemaClassDao;
    @Autowired
    UserSchemaPropertyDao userSchemaPropertyDao;
    @Autowired
    HttpClientTemplate template;
    @Value("${api.clearSchema.path}")
    private String cleadSchemaPath;
    @Value("${api.host}")
    private String apiHost;

    public UserSchemaManageServiceImpl() {
    }

    public JsonNode clearSchemaCache() throws IOException {
        String url = this.apiHost + this.cleadSchemaPath;
        log.info("clearSchemaCache url-------->{}", url);
        String response = this.template.executePostData(url, (Map)null, (String)null);
        log.info("clearSchemaCache response-------->{}", response);
        JsonNode responseData = JacksonUtils.toJsonNode(response);
        return responseData;
    }

    @Transactional
    public Integer insertSchemaClass(UserSchemaClass schemaClass) {
        this.userSchemaClassDao.insert(schemaClass);
        return schemaClass.getId();
    }

    @Transactional
    public void updateSchemaClass(UserSchemaClass schemaClass) {
        String oldId = this.userSchemaClassDao.getOneById(schemaClass.getId()).getAtId();
        this.userSchemaClassDao.update(schemaClass);
        this.userSchemaClassDao.updateSubClassByAtId(schemaClass.getAtId(), oldId);
        this.userSchemaPropertyDao.updateDomain(schemaClass.getAtId(), oldId);
    }

    @Transactional
    public void deleteSchemaClass(Integer id) {
        this.userSchemaPropertyDao.deleteByClassId(id);
        this.userSchemaClassDao.deleteById(id);
    }

    @Transactional
    public Integer insertSchemaProperty(UserSchemaProperty schemaProperty) {
        this.userSchemaPropertyDao.insert(schemaProperty);
        return schemaProperty.getId();
    }

    @Transactional
    public void deleteSchemaPropertyById(Integer id) {
        this.userSchemaPropertyDao.deleteById(id);
    }

    @Transactional
    public void deleteSchemaPropertyByDomain(String domain) {
        this.userSchemaPropertyDao.deleteByDomain(domain);
    }

    @Transactional
    public void updateSchemaProperty(UserSchemaProperty schemaProperty) {
        this.userSchemaPropertyDao.update(schemaProperty);
    }
}
